package com.mrobuy.image;

import com.jingsu.ImageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Controller
public class ImageCheckController {

    @RequestMapping("/imagecheck")
    public String imagecheck() {
        return  "imagecheck";
    }

    @RequestMapping("/check")
    @ResponseBody
    public ImageInfo check(HttpServletRequest request) {

       String imageUrl =  ServletRequestUtils.getStringParameter(request, "url", "");
       if ( StringUtils.isEmpty(imageUrl) ) {
            return ImageInfo.NO_EXIST;
       }

       if ( !imageUrl.contains("https://static.mymro.cn") ) {
           return ImageInfo.NO_EXIST;
       }


       String path = imageUrl.replace("https://static.mymro.cn", "/home/images");

       File exist = new File(path);

        ImageInfo imageInfo = new ImageInfo();

        imageInfo.setExist(exist.exists());
        imageInfo.setUrl(exist.getAbsolutePath().replace("/home/", ""));

        return  imageInfo;
    }

}
